﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.QM;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.IO.Compression;
using System.Net;
using System.Data.OleDb;
using System.Collections;
using System.Reflection;
namespace XJLSC.QM
{
    public partial class SCBG : Form
    {
        private readonly DataBase db = new DataBase();
        public SCBG()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

                 
                string strSql = null;
                strSql = "SELECT TY001,TY002,TA006,MB003,MW002,TY003,RTRIM(TY009) TY009,RTRIM(TY019) TY019,(CASE WHEN RTRIM(ISNULL(MOCTY.UDF02,''))='已报工' THEN CEILING(TY010) ELSE CEILING(TY004) END) TY004,CEILING(MOCTY.UDF51) RYSL,CEILING(MOCTY.UDF52) GZSS,CEILING(MOCTY.UDF53) ZGS,CEILING(TY006) TY006,ISNULL(MOCTY.UDF01,'') BZ,(CASE WHEN TY006=0 AND RTRIM(ISNULL(MOCTY.UDF02,''))='已报工' THEN 'true' else 'false' END) AS SFWC FROM MOCTY LEFT JOIN MOCTA ON TY001=TA001 AND TY002=TA002  LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN CMSMW ON MW001=TY019 WHERE TY003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA021='" + textBox1.Text + "' AND (MW002='" + comboBox1.Text + "' OR '" + comboBox1.Text + "'='')";
             
          
            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.dataGridView1.Columns["MB003"].Width = 300;
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView1.AllowUserToAddRows = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }

            double sum = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum += double.Parse(this.dataGridView1.Rows[i].Cells["TY004"].Value.ToString());
            }
            textBox2.Text = sum.ToString();

            double sum1 = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum1 += double.Parse(this.dataGridView1.Rows[i].Cells["TY006"].Value.ToString());
            }
            textBox3.Text = sum1.ToString();
        }

        private void SCBG_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (PropertyClass.jiaose != "车间统计员")
            {
                MessageBox.Show("对不起,您没有报工权限", "软件提示");
                return;
            }
            try
            {
                string strSql = null;
                foreach (DataGridViewRow dr in this.dataGridView1.Rows)
                {

                    if (Convert.ToDouble(dr.Cells["TY006"].Value) > 0)
                    {
                        //MessageBox.Show(dr.Index.ToString(), "软件提示");
                        strSql = "UPDATE MOCTY SET TY010=" + dr.Cells["TY004"].Value + ",TY006=" + dr.Cells["TY006"].Value + ",UDF51=" + dr.Cells["RYSL"].Value + ",UDF52=" + dr.Cells["GZSS"].Value + ",UDF53=" + dr.Cells["ZGS"].Value + ",UDF01='" + dr.Cells["BZ"].Value + "',UDF02='已报工' WHERE TY001='" + dr.Cells["TY001"].Value + "' AND TY002='" + dr.Cells["TY002"].Value + "' AND TY003='" + dr.Cells["TY003"].Value + "' AND TY009='" + dr.Cells["TY009"].Value + "' AND TY019='" + dr.Cells["TY019"].Value + "'";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {
                                MessageBox.Show("报工失败，请检查", "软件提示");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }

                        strSql = "UPDATE MOCTY SET TY004=" + dr.Cells["TY006"].Value + " WHERE TY001='" + dr.Cells["TY001"].Value + "' AND TY002='" + dr.Cells["TY002"].Value + "' AND TY003='" + dr.Cells["TY003"].Value + "' AND TY009='" + dr.Cells["TY009"].Value + "' AND TY019='" + dr.Cells["TY019"].Value + "'";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {
                                MessageBox.Show("报工失败，请检查", "软件提示");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                    }

                    if (Convert.ToBoolean(dr.Cells["SFWC"].Value))
                    {
                        strSql = "UPDATE MOCTY SET TY010=" + dr.Cells["TY004"].Value + ",TY006=0,UDF51=0,UDF52=0,UDF53=0,UDF01='" + dr.Cells["BZ"].Value + "',UDF02='已报工' WHERE TY001='" + dr.Cells["TY001"].Value + "' AND TY002='" + dr.Cells["TY002"].Value + "' AND TY003='" + dr.Cells["TY003"].Value + "' AND TY009='" + dr.Cells["TY009"].Value + "' AND TY019='" + dr.Cells["TY019"].Value + "'";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {
                                MessageBox.Show("报工失败，请检查", "软件提示");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }

                        strSql = "UPDATE MOCTY SET TY004=0 WHERE TY001='" + dr.Cells["TY001"].Value + "' AND TY002='" + dr.Cells["TY002"].Value + "' AND TY003='" + dr.Cells["TY003"].Value + "' AND TY009='" + dr.Cells["TY009"].Value + "' AND TY019='" + dr.Cells["TY019"].Value + "'";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {
                                MessageBox.Show("报工失败，请检查", "软件提示");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                    }
                }

                MessageBox.Show("报工成功！", "软件提示");
                DataTable dt = (DataTable)dataGridView1.DataSource;
                dt.Rows.Clear();
                dataGridView1.DataSource = dt;

            }
                
                  catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

        
            
 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }

        }
            }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1 && (e.ColumnIndex== this.dataGridView1.Columns["TY004"].Index))
                {
                    double sum = 0;
                    for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
                    {
                        sum += double.Parse(this.dataGridView1.Rows[i].Cells["TY004"].Value.ToString());
                    }
                    textBox2.Text = sum.ToString();

                    
                }
                if (e.RowIndex > -1 && (e.ColumnIndex== this.dataGridView1.Columns["TY006"].Index))
                {
                      double sum1 = 0;
                    for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
                    {
                        sum1 += double.Parse(this.dataGridView1.Rows[i].Cells["TY006"].Value.ToString());
                    }
                    textBox3.Text = sum1.ToString();
                }
                if (e.RowIndex > -1 && (e.ColumnIndex == this.dataGridView1.Columns["RYSL"].Index || e.ColumnIndex == this.dataGridView1.Columns["GZSS"].Index))
                {
                    double zgs= Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["RYSL"].Index].Value) * Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["GZSS"].Index].Value);
                    this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["ZGS"].Index].Value = zgs.ToString();
                       
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
